﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SharePointOptimizer.PL.WPF.Properties;
using System.Configuration;
using AG.Utilities;

namespace SharePointOptimizer.PL.WPF
{
    /// <summary>
    /// Interaction logic for OptionDialog.xaml
    /// </summary>
    public partial class OptionDialog : Window
    {
        MainWindow mainWindow;


        public OptionDialog(MainWindow sender)
        {
            InitializeComponent();

            mainWindow = sender;
        }

        private void btnSaveOptionsOK_Click(object sender, RoutedEventArgs e)
        {

            Properties.Settings.Default.Save();

            if (!String.IsNullOrEmpty(txtEmail.Text) && !String.IsNullOrEmpty(txtProductKey.Text))
            {
                if (Licensing.IsValidProductKey(txtEmail.Text.Trim().ToLower(), txtProductKey.Text.Trim().ToUpper(), typeof(MainWindow).Assembly))
                {
                    if (Licensing.Edition == ProductEdition.Basic)
                    {
                        MessageBox.Show("Product Key is valid, thank you for purchasing the Premium Edition!");
                        Licensing.SaveProductKey(txtEmail.Text, txtProductKey.Text);
                        Licensing.Edition = ProductEdition.Premium;
                    }
                    this.DialogResult = true;
                }
                else
                {
                    Licensing.Edition = ProductEdition.Basic;
                    MessageBox.Show("Product Key is invalid for this E-mail address. Have you installed a newer version? If so request a new Product Key by sending an E-mail to info@arthurgraus.nl.");
                }

                mainWindow.SetVisibilityByProductEdition();
            }
            else
            { this.DialogResult = true; }


        }

        private void btnOptionCancel_Click(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.Reload();
            this.Close();
           
           
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            System.Configuration.Configuration oConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            txtProductKey.Text= oConfig.AppSettings.Settings["ProductKey"].Value;
            txtEmail.Text = oConfig.AppSettings.Settings["Email"].Value;

        }
    }
}

